package com.harmony.pilot.completion.controller;

import com.harmony.pilot.common.dto.CompletionItem;
import com.harmony.pilot.common.dto.CompletionRequest;
import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.completion.service.CompletionService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代码补全控制器
 */
@Slf4j
@RestController
@RequestMapping("/completion")
@RequiredArgsConstructor
public class CompletionController {
    
    private final CompletionService completionService;
    
    /**
     * 获取代码补全建议
     */
    @PostMapping("/suggest")
    public RestResult<List<CompletionItem>> getCompletions(
            @Valid @RequestBody CompletionRequest request) {
        log.info("Received completion request for file: {}", 
                request.getContext().getFilePath());
        
        try {
            List<CompletionItem> items = completionService.getCompletions(request);
            return RestResult.success(items);
        } catch (Exception e) {
            log.error("Failed to get completions", e);
            return RestResult.error("Failed to get completions: " + e.getMessage());
        }
    }
    
    /**
     * 获取C++函数补全
     */
    @PostMapping("/function")
    public RestResult<List<CompletionItem>> getFunctionCompletions(
            @Valid @RequestBody CompletionRequest request) {
        log.info("Received function completion request");
        
        try {
            List<CompletionItem> items = completionService.getFunctionCompletions(request);
            return RestResult.success(items);
        } catch (Exception e) {
            log.error("Failed to get function completions", e);
            return RestResult.error("Failed to get function completions: " + e.getMessage());
        }
    }
    
    /**
     * 获取GN构建文件补全
     */
    @PostMapping("/gn")
    public RestResult<List<CompletionItem>> getGnCompletions(
            @Valid @RequestBody CompletionRequest request) {
        log.info("Received GN completion request");
        
        try {
            List<CompletionItem> items = completionService.getGnCompletions(request);
            return RestResult.success(items);
        } catch (Exception e) {
            log.error("Failed to get GN completions", e);
            return RestResult.error("Failed to get GN completions: " + e.getMessage());
        }
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public RestResult<String> health() {
        return RestResult.success("Code Completion Service is running");
    }
}

